﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DynamicText.Components.Presenters
{
    using DotNetNuke.Common.Utilities;

    using DynamicText.Components.Controller;
    using DynamicText.Components.Views;

    using YourCompany.Modules.DynamicText;

    public class EditDynamicTextPresenter
    {
        #region "Private Members"
        private IEditDynamicText View;
        #endregion
        private IDynamicTextController Controller;

        #region "Page Events"
        public EditDynamicTextPresenter(IEditDynamicText _View)
        {
            View = _View;
            Controller = new DynamicTextController();
        }

        public EditDynamicTextPresenter(IEditDynamicText _View, IDynamicTextController _Controller)
        {
            View = _View;
            Controller = _Controller;
        }

        public void OnViewLoad(bool LoadForm)
        {
            try
            {
                //' If this is the first visit to the page, bind the role data to the datalist

                if (LoadForm == false)
                {
                    if (!DotNetNuke.Common.Utilities.Null.IsNull(View.ItemId))
                    {
                        // get content
                        DynamicTextInfo objDynamicText = Controller.GetDynamicText(View.ModuleId, View.ItemId);
                        if ((objDynamicText != null))
                        {
                            View.EditorContent = objDynamicText.Content;
                            View.AuditCreatedByUser = objDynamicText.CreatedByUserName;
                            View.AuditCreatedDate = objDynamicText.CreatedDate.ToString();
                            // security violation attempt to access item not related to this Module
                        }
                        else
                        {
                            View.SecurityViolation = true;
                        }
                    }
                    else
                    {
                        View.DeleteButtonVisible = false;
                        View.AuditControlVisible = false;
                    }
                }
            }
            catch (Exception exc)
            {
                View.Exception = exc;
            }
        }

        public void OnCancel()
        {
            View.Redirect = true;
        }

        public void OnUpdate()
        {

            try
            {
                DynamicTextInfo objDynamicText = new DynamicTextInfo();

                objDynamicText.ModuleId = View.ModuleId;
                objDynamicText.ItemId = View.ItemId;
                objDynamicText.Content = View.EditorContent;
                objDynamicText.CreatedByUser = View.UserId;

                if (DotNetNuke.Common.Utilities.Null.IsNull(View.ItemId))
                {
                    // add the content within the DynamicText table
                    int ItemId = Controller.AddDynamicText(objDynamicText);
                    if (ItemId == Null.NullInteger)
                    {
                        throw new Exception("Add Exception");
                    }
                }
                else
                {
                    // update the content within the DynamicText table
                    Controller.UpdateDynamicText(objDynamicText);
                }

                // Redirect back to the portal home page
                View.Redirect = true;
                //Module failed to load
            }
            catch (Exception exc)
            {
                View.Exception = exc;
            }
        }

        public void OnDelete()
        {
            try
            {
                // Only attempt to delete the item if it exists already
                DynamicTextInfo objDynamicText = Controller.GetDynamicText(View.ModuleId, View.ItemId);
                if ((objDynamicText != null))
                {
                    Controller.DeleteDynamicText(View.ModuleId, View.ItemId);
                    // Redirect back to the portal home page
                    View.Redirect = true;
                    // security violation attempt to access item not related to this Module
                }
                else
                {
                    View.SecurityViolation = true;
                }
            }
            catch (Exception exc)
            {
                View.Exception = exc;
            }
        }

        #endregion
    }
}